<?php
/**
 * Enter description here...
 *
 * @表__yuyuetype typeid-typename: 0-科室 1-医生 
 * @package defaultPackage
 * @date 	$Date: 2010/05/04 04:06:38 $
 */
!defined('ADMIN') && exit('Not allowed to directly view page');

if(empty($action))
{
	$sectors = $topsectors = $subsectors = array();
	$sectorscache = '';
	$sql = "select * from `#@__yy_type`  order by disorder asc, id asc";
	$dsql->SetQuery($sql);
	$dsql->Execute();
	while($row = $dsql->GetArray())
	{	
		if($row['fid'] == 0)
		{
			$topsectors[] = $row;
		}
		else
		{
			$subsectors[] = $row;
		}
	}
	
	foreach($topsectors as $topsector)
	{
		$sectors[] = $topsector;
		$sectorscache .= '<option value="'.$topsector['id'].'">|- '.$topsector['name'].'</option>';
		foreach($subsectors as $subsector)
		{
			if($subsector['fid'] == $topsector['id'])
			{
				$sectors[] = $subsector;
			}
		}
	}
	
	include(DEDEADMIN."/templets/yuyue_type.htm");
	exit();
}
else if($action == 'add')
{
	$name = trim($name);
	if($name == '')
	{
		ShowMsg('分类名称不能为空','?do=yuyue_type');
		exit();
	}
	$sql = "insert into `#@__yy_type`(name, fid) values('$name','$fid'); ";
	if($dsql->ExecuteNoneQuery($sql))
	{
		ShowMsg('添加分类成功，将返回分类管理页面','?do=yuyue_type');
		exit();
	}
	else
	{
		ShowMsg('添加分类失败，将返回分类管理页面','?do=yuyue_type');
		exit();
	}
}
elseif($action == 'edit')
{
	if(empty($step))
	{
		$sectorscache = '<option value="0">无(作为一级分类)</option>';
		$sql = "select * from `#@__yy_type` where id='$id' ";
		$sector = $dsql->GetOne($sql);
		$sql = "select * from `#@__yy_type` where fid=0 and id<>'$id' order by disorder asc, id asc";
		$dsql->SetQuery($sql);
		$dsql->Execute();
		while($topsector = $dsql->GetArray())
		{
			$check = '';
			if($sector['fid'] != 0 && $topsector['id'] == $sector['fid'])
			{
				$check = 'selected';
			}
			$sectorscache .= '<option value="'.$topsector['id'].'" '. $check.'>'.$topsector['name'].'</option>';
		}		
		include(DEDEADMIN."/templets/yuyue_type.htm");
		exit();
	}
	else if($step == 2)
	{
		$sql = "update `#@__yy_type` set name='$name', fid='$fid', disorder='$disorder' ,doctor='$doctor' where id='$id' ";
		if($dsql->ExecuteNoneQuery($sql))
		{
			ShowMsg('编辑分类成功，将返回分类管理页面','?do=yuyue_type');
			exit();
		}
		else
		{
			ShowMsg('编辑分类成功，将返回分类管理页面','?do=yuyue_type');
			exit();
		}
	}
}
else if($action == 'update')
{
	$errinfo = '';
	foreach($disorders as $key => $disorder)
	{
		$sql = "update `#@__yy_type` set disorder='$disorder', name='$names[$key]' ,doctor='$doctors[$key]' where id='$key' ";
		if(!$dsql->ExecuteNoneQuery($sql))
		{
			$errinfo .= $sql."\n";
		}
	}
	if(trim($errinfo)  != '' )
	{
		ShowMsg($errinfo,'?do=yuyue_type');
		exit();
	}
	else
	{
		ShowMsg('更新分类成功，将返回分类管理页面','?do=yuyue_type');
		exit();
	}
}
elseif($action == 'delete')
{
	if(empty($step))
	{
		include(DEDEADMIN.'/templets/yuyue_type.htm');
		exit();
	}
	else if($step == 2)
	{
		$id = intval($id);
		if($id < 1)
		{
			ShowMsg('分类编号不正确，将返回分类管理页面','?do=yuyue_type');
			exit();
		}
		else
		{
			$sql = "delete from `#@__yy_type` where id='$id' or fid='$id' ";
			if($dsql->ExecuteNoneQuery($sql))
			{
				ShowMsg('删除分类成功，将返回分类管理页面', '?do=yuyue_type');
				exit();
			}
			else
			{
				ShowMsg('删除分类失败，将返回分类管理页面 ','?do=yuyue_type');
				exit();
			}
		}
	}
}
?>